/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmQuanLyDanhMuc.java
 *
 * Created on Aug 22, 2012, 3:08:38 PM
 */
package gui;

import dao.DanhMucDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Danhmuc;

/**
 *
 * @author dtth
 */
public class frmQuanLyDanhMuc extends javax.swing.JFrame {

    private int _irow;
    private List<Danhmuc> _ldm;
    
    /** Creates new form frmQuanLyDanhMuc */
    public frmQuanLyDanhMuc() {
        initComponents();
       
        _irow = 0;
        this._ldm = null;
                
        //dua du lieu vao table
        this._ldm = DanhMucDAO.layDanhSachDanhMuc();
        fillJTable(_ldm);

        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTenDanhMuc = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbDanhMuc = new javax.swing.JTable();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtMaDanhMuc = new javax.swing.JTextField();
        btnThoat = new javax.swing.JButton();
        jbtnTimKiem = new javax.swing.JButton();
        jbtnLamMoi = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("QUẢN LÝ DANH MỤC");

        jLabel4.setText("Tên Danh Mục");

        tbDanhMuc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbDanhMuc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhMucMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tbDanhMucMouseEntered(evt);
            }
        });
        tbDanhMuc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbDanhMucKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(tbDanhMuc);

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        jLabel5.setText("Mã Danh Mục");

        txtMaDanhMuc.setEditable(false);
        txtMaDanhMuc.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        btnThoat.setText("Đóng");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jbtnTimKiem.setText("Tìm kiếm");
        jbtnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnTimKiemActionPerformed(evt);
            }
        });

        jbtnLamMoi.setText("Làm Mới");
        jbtnLamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLamMoiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(176, 176, 176)
                .addComponent(jLabel2))
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(jLabel5)
                .addGap(30, 30, 30)
                .addComponent(txtMaDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(jLabel4)
                .addGap(26, 26, 26)
                .addComponent(txtTenDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(259, 259, 259)
                .addComponent(jbtnLamMoi)
                .addGap(18, 18, 18)
                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(130, Short.MAX_VALUE)
                .addComponent(jbtnTimKiem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel2)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel5))
                    .addComponent(txtMaDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4))
                    .addComponent(txtTenDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnSua)
                        .addComponent(btnXoa))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnThem)
                        .addComponent(jbtnTimKiem)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThoat)
                    .addComponent(jbtnLamMoi))
                .addGap(18, 18, 18))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
// TODO add your handling code here:
    Danhmuc dm = new Danhmuc();
    //dm.setMadanhmuc(7);
    dm.setTendanhmuc(this.txtTenDanhMuc.getText().toString());
    boolean kq = DanhMucDAO.themDanhMuc(dm);
    if (kq) {
        JOptionPane.showMessageDialog(this, "Thêm thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, "Không thêm đươc !", "Thông báo", JOptionPane.WARNING_MESSAGE);
    }
    //dua du lieu vao table
    fillJTable(_ldm);
    this.txtMaDanhMuc.setText("");
    this.txtTenDanhMuc.setText("");
}//GEN-LAST:event_btnThemActionPerformed

private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
// TODO add your handling code here:
    //update danhmuc set tendanhmuc = 'bbbb' where tendanhmuc = 'aaaa'
    Danhmuc dm = new Danhmuc();
    dm.setMadanhmuc(Integer.parseInt(this.txtMaDanhMuc.getText()));
    dm.setTendanhmuc(this.txtTenDanhMuc.getText().toString());
    boolean kq = DanhMucDAO.capnhatThongTinDanhMuc(dm);
    if (kq) {
        JOptionPane.showMessageDialog(this, "Cập nhật thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, "Cập nhật không được !", "Thông báo", JOptionPane.WARNING_MESSAGE);
    }
    //dua du lieu vao table
    fillJTable(_ldm);
    
    //lây du lieu tai dong dang chon trong table dua vao cac textfield
    this.txtMaDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 0).toString());
    this.txtTenDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 1).toString());
}//GEN-LAST:event_btnSuaActionPerformed

private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
// TODO add your handling code here:
    Danhmuc dm = new Danhmuc();
    dm.setMadanhmuc(Integer.parseInt(this.txtMaDanhMuc.getText()));
    dm.setTendanhmuc(this.txtTenDanhMuc.getText().toString());
    boolean kq = DanhMucDAO.xoaDanhMuc(dm);
    if (kq) {
        JOptionPane.showMessageDialog(this, "Xóa thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, "Xóa không được !", "Thông báo", JOptionPane.WARNING_MESSAGE);
    }
    //dua du lieu vao table
    fillJTable(_ldm);
    
    //lây du lieu tai dong dang chon trong table dua vao cac textfield
    this.txtMaDanhMuc.setText("");
    this.txtTenDanhMuc.setText("");
}//GEN-LAST:event_btnXoaActionPerformed

private void tbDanhMucKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDanhMucKeyPressed
// TODO add your handling code here:
    int key = evt.getKeyCode();
    //JOptionPane.showMessageDialog(this, key, "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    _irow = this.tbDanhMuc.getSelectedRow();
    if (key == 40) {//nhan nut xuong
        _irow++;
    }
    if (key == 38) {//nhan nutlen
        _irow--;
    }
    //lây du lieu tai dong dang chon trong table dua vao cac textfield
    this.txtMaDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 0).toString());
    this.txtTenDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 1).toString());
}//GEN-LAST:event_tbDanhMucKeyPressed

private void tbDanhMucMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhMucMouseEntered
// TODO add your handling code here:
}//GEN-LAST:event_tbDanhMucMouseEntered

private void tbDanhMucMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhMucMouseClicked
// TODO add your handling code here:
    _irow = this.tbDanhMuc.getSelectedRow();
    //lây du lieu tai dong dang chon trong table dua vao cac textfield
    this.txtMaDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 0).toString());
    this.txtTenDanhMuc.setText(this.tbDanhMuc.getModel().getValueAt(_irow, 1).toString());
}//GEN-LAST:event_tbDanhMucMouseClicked

private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
// TODO add your handling code here:
//    System.exit(1);
    this.setVisible(false);
}//GEN-LAST:event_btnThoatActionPerformed

private void jbtnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimKiemActionPerformed
// TODO add your handling code here:
    List<Danhmuc> ldm = null;
    ldm = DanhMucDAO.timKiemDanhMuc(this.txtTenDanhMuc.getText());
    if (ldm.size() > 0) {
        fillJTable(ldm);
    } else {
        JOptionPane.showMessageDialog(this, "Không tìm thấy !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_jbtnTimKiemActionPerformed

private void jbtnLamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLamMoiActionPerformed
// TODO add your handling code here:
    //dua du lieu vao table
    fillJTable(_ldm);
    this.txtMaDanhMuc.setText("");
    this.txtTenDanhMuc.setText("");
}//GEN-LAST:event_jbtnLamMoiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmQuanLyDanhMuc().setVisible(true);
            }
        });
    }
    
    private void fillJTable(List<Danhmuc> ldm){
        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.addColumn("Mã Danh Mục");
        tableModel.addColumn("Tên Danh Mục");
        this.tbDanhMuc.setModel(tableModel);
        
        for (int i = 0; i < ldm.size(); i++) {
            Vector vt = new Vector();
            vt.add(ldm.get(i).getMadanhmuc());
            vt.add(ldm.get(i).getTendanhmuc());

            tableModel.addRow(vt);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtnLamMoi;
    private javax.swing.JButton jbtnTimKiem;
    private javax.swing.JTable tbDanhMuc;
    private javax.swing.JTextField txtMaDanhMuc;
    private javax.swing.JTextField txtTenDanhMuc;
    // End of variables declaration//GEN-END:variables
}
